<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\DepartmentShowAction;
use App\Models\DepartmentCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Tree;
use Dcat\Admin\Widgets\Box;
use Dcat\Admin\Widgets\Form as WidgetForm;

class DepartmentCategoryController extends AdminController
{


    public function index(Content $content)
    {
        return $content
            ->translation($this->translation())
            ->title($this->title())
            ->description($this->description()['index'] ?? trans('admin.list'))
            ->body(function (Row $row) {
                $row->column(7, $this->treeView()->render());

                $row->column(5, function (Column $column) {
                    $form = new WidgetForm();
                    $form->action(admin_url('department_categories'));

                    $options = DepartmentCategory::oneLevelOptions();

                    $form->select('parent_id')->options($options)->default(0);
                    $form->text('title')->required();

                    $form->width(9, 2);

                    $column->append(Box::make(trans('admin.new'), $form));
                });
            });
    }

    /**
     * @return \Dcat\Admin\Tree
     */
    protected function treeView()
    {

        return new Tree(new DepartmentCategory(), function (Tree $tree) {
            $tree->disableCreateButton();
            $tree->disableQuickCreateButton();
            $tree->disableEditButton();
            $tree->maxDepth(3);

            $tree->actions(function (Tree\Actions $actions) {
                if ($actions->getRow()->extension) {
                    $actions->disableDelete();
                }

                $actions->prepend(new DepartmentShowAction());
            });

            $tree->branch(function ($branch) {
                $img = "";
                if ($branch["parent_id"] && !isset($branch['children'])) {
                    $img = "<img src='/storage/{$branch['icon']}' style='display:inline-block; height: 50px;width:50px;object-fit: cover;'/>";
                }
                $payload = "<div style='display:inline-block;'>{$img}&nbsp;&nbsp;&nbsp;<strong>{$branch['title']}</strong></div>";
                return $payload;
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    public function form()
    {

        return Form::make(new DepartmentCategory(), function (Form $form) {
            $form->tools(function (Form\Tools $tools) {
                $tools->disableView();
            });

            $model =$form->model();

            $form->display('id', 'ID');
            $options = DepartmentCategory::oneLevelOptions();

            $form->select('parent_id')
                ->options($options)
                ->default(0)
                ->saving(function ($v) {
                    return (int)$v;
                });
            $form->text('title')->required();
            $form->switch('is_show');

            if (!request()->has("_order") && $model["parent_id"] &&  !$model->childrenRelation->count()) {
                $form->file("icon")
                    ->removable(false)
                    ->autoUpload();
                $form->editor("reservation_notify");
            }

            $form->hidden('depth');
            $form->display('created_at', trans('admin.created_at'));
            $form->display('updated_at', trans('admin.updated_at'));

            $form->submitted(function (Form $form) {
                if (!request()->has("_order")) {
                    if ($pId = $form->input("parent_id")) {
                        $department = DepartmentCategory::query()->select(['id', 'depth'])->where("id", $pId)->first();
                        $form->input('depth', $department->depth + 1);
                    } else {
                        $form->input('depth', 1);
                    }
                }
            });


        })->saved(function (Form $form, $result) {
            $response = $form->response()->location('department_categories');

            if ($result) {
                return $response->success(__('admin.save_succeeded'));
            }

            return $response->info(__('admin.nothing_updated'));
        });
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new DepartmentCategory(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('title');
            $grid->column('parent_id');
            $grid->column('is_show');
            $grid->column('order');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new DepartmentCategory(), function (Show $show) {
            $show->field('id');
            $show->field('title');
            $show->field('parent_id');
            $show->field('is_show');
            $show->field('order');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

}
